package comc.springbatch;

import comc.pojo.LlAppyInfo;
import comc.pojo.User;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DbReader {
    @Autowired
    private DataSource dataSource;

    @Bean
    public ItemReader<LlAppyInfo> myReader2() {
        JdbcPagingItemReader<LlAppyInfo> reader=new JdbcPagingItemReader<>();
        reader.setDataSource(dataSource);
//        每次读取的大小
        reader.setFetchSize(2);
        reader.setPageSize(2);
//        把读取到数据封装成User对象
        reader.setRowMapper(new RowMapper<LlAppyInfo>() {
            @Override
            public LlAppyInfo mapRow(ResultSet result, int i) throws SQLException {
                LlAppyInfo u =new LlAppyInfo();
                u.setAid(Integer.parseInt(result.getString(1)));
                u.setBatchNo(result.getString(2));
                u.setAcceptNo(result.getString(3));
                System.out.println("读取到的数据"+u.toString());
                return u;
            }
        });
//        指定sql语句
        MySqlPagingQueryProvider mySqlPagingQueryProvider=new MySqlPagingQueryProvider();
        mySqlPagingQueryProvider.setSelectClause("aid,aname,acode ");
        mySqlPagingQueryProvider.setFromClause("from a");
//        指定根据那个字段排行
        Map<String, Order> stringOrderMap=new HashMap<>(1);
        stringOrderMap.put("aid", Order.ASCENDING);
        mySqlPagingQueryProvider.setSortKeys(stringOrderMap);
        reader.setQueryProvider(mySqlPagingQueryProvider);
        reader.close();
        return reader;
    }
}
